package com.crazy.boot.service.impl;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crazy.boot.entity.User;
import com.crazy.boot.mapper.RoleMapper;
import com.crazy.boot.mapper.UserMapper;
import com.crazy.boot.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Override
	public User getUserByName(String name) {
		User user = userMapper.getUserByName(name);
		if(user == null) {
			return null;
		}
		user.setRList(roleMapper.getRoleByUid(user.getUid()));
		return user;
	}

	@Override
	public User login(String name, String password, boolean rememberMe) {
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(name, password, rememberMe);
		subject.login(token);
		return this.getUserByName(name);
	}

	@Override
	public List<User> listUser() {
		List<User> uList = userMapper.listUser();
		if(uList.size() > 0) {
			for(User u : uList) {
				u.setRList(roleMapper.getRoleByUid(u.getUid()));
			}
		}
		return uList;
	}

}
